//
// Created by QU on 24-9-15.
//

#include<iostream>
#include<string>

#include "nlohmann/json.hpp"

using json = nlohmann::json;
namespace json_literals = nlohmann::json_literals;

int main(int argc, char* argv[])
{
    json json_obj;

    // std::ifstream f("example.json");
    // json data = json::parse(f);

    //// create json object
    // Using (raw) string literals and json::parse
    json ex1 = json::parse(R"(
  {
    "pi": 3.141,
    "happy": true
  }
)");

    // Using user-defined (raw) string literals
    using namespace nlohmann::literals;
    json ex2 = R"(
  {
    "pi": 3.141,
    "happy": true
  }
)"_json;

    // Using initializer lists
    json ex3 = {
        { "happy", true },
        { "pi", 3.141 },
    };

    /*假设您要创建 JSON 对象
    {
        "pi": 3.141,
        "happy": true,
        "name": "Niels",
        "nothing": null,
        "answer": {
            "everything": 42
          },
          "list": [1, 0, 2],
          "object": {
              "currency": "USD",
              "value": 42.99
            }
    }*/

    // create an empty structure (null)
    json j;

    // add a number that is stored as double (note the implicit conversion of j to an object)
    j["pi"] = 3.141;

    // add a Boolean that is stored as bool
    j["happy"] = true;

    // add a string that is stored as std::string
    j["name"] = "Niels";

    // add another null object by passing nullptr
    j["nothing"] = nullptr;

    // add an object inside the object
    j["answer"]["everything"] = 42;

    // add an array that is stored as std::vector (using an initializer list)
    j["list"] = { 1, 0, 2 };

    // add another object (using an initializer list of pairs)
    j["object"] = { { "currency", "USD" }, { "value", 42.99 } };

    // instead, you could also write (which looks very similar to the JSON above)
    json j2 = {
        { "pi", 3.141 },
        { "happy", true },
        { "name", "Niels" },
        { "nothing", nullptr },
        {
            "answer", {
                { "everything", 42 }
            }
        },
        { "list", { 1, 0, 2 } },
        {
            "object", {
                { "currency", "USD" },
                { "value", 42.99 }
            }
        },
        // a way to express the empty array []
        json::array(),
        // ways to express the empty object {}
        json::object(),
    };

    // parse json_object
    std::string j2_str { j2.dump() };
    std::cout << "j2.dump()     \tstr:" << std::endl;
    std::cout << j2_str << std::endl;

    std::cout << "cout << j2;   \tstr:" << std::endl;
    std::cout << j2 << std::endl;

}
